package com.md.value;

import com.md.base.EnumBase;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 异常规则: <p/>在一个 module 下 code 递增(1),不同 module 下 code 递增(10000)
 *
 * @author 刘骄阳
 * 2022-09-21 21:35
 */
@Getter
@AllArgsConstructor
public enum ErrorEnum implements EnumBase {
  PERMISSION(403, "权限不足!"),
  UNAUTHORIZED(401, "登录过期!"),
  UN_EXISTENT(404, "地址不存在!"),
  SYSTEM_ERROR(500, "服务异常!"),
  USER_UNAVAILABLE(10000, "账号解冻剩余: %s!");

  private static final Map<Integer, ErrorEnum> ENUM_MAP = Arrays.stream(ErrorEnum.values())
    .collect(Collectors.toMap(EnumBase::getCode, e -> e));
  private final Integer code;
  private final String message;

  public static ErrorEnum of(final Integer code) {
    return ENUM_MAP.get(code);
  }
}
